# Python 2.6.4
# Project Euler, Problem 134
# Copyright 2010 Talha Zaman

from mine import sieveprimes
table = [[0]*10 for i in range(10)]
for i in range(1, 10):
    for j in range(10):
        table[i][(i*j)%10] = j
def S(p1, p2):
    digit, mult = 0, 0
    while p1:
        mult += table[p2%10][(p1%10-(p2*mult/10**digit)%10)%10] * 10**digit
        p1, digit = p1/10, digit+1
    return p2*mult
p = sieveprimes(1000010)
print sum(S(p[i], p[i+1]) for i in range(2, len(p)-1))
